package com.pda.yuanjing.pages.kccx;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.KCTJBeanOut;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ItemAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater inflater;
    List<KCTJBeanOut.DataBean> mlists = new ArrayList<>();
    private int selectedPosition = -1;

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    public ItemAdapter(Context context, List<KCTJBeanOut.DataBean> lists) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        mlists = lists;
    }

    @Override
    public int getCount() {
        return mlists == null ? 0 : mlists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_codeq, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv1.setText("");
        viewHolder.tv2.setText("");
        for (int i = 0; i < mlists.get(position).getListResult().size(); i++) {
            if (i == mlists.get(position).getListResult().size() - 1) {
                viewHolder.tv1.append(mlists.get(position).getListResult().get(i).getKey());
                viewHolder.tv2.append(mlists.get(position).getListResult().get(i).getValue());
            } else {
                if(mlists.get(position).getListResult().get(i).getValue().length()<=20){
                    viewHolder.tv1.append(mlists.get(position).getListResult().get(i).getKey()+"\n");
                    viewHolder.tv2.append(mlists.get(position).getListResult().get(i).getValue()+"\n");
                }
                else if(mlists.get(position).getListResult().get(i).getValue().length()>20 && mlists.get(position).getListResult().get(i).getValue().length()<=40){
                    viewHolder.tv1.append(mlists.get(position).getListResult().get(i).getKey()+"\n\n");
                    viewHolder.tv2.append(mlists.get(position).getListResult().get(i).getValue().substring(0,21)+"\n");
                    viewHolder.tv2.append(mlists.get(position).getListResult().get(i).getValue().substring(21)+"\n");
                }
                else if(mlists.get(position).getListResult().get(i).getValue().length()>40 && mlists.get(position).getListResult().get(i).getValue().length()<=60){
                    viewHolder.tv1.append(mlists.get(position).getListResult().get(i).getKey()+"\n\n\n");
                    viewHolder.tv2.append(mlists.get(position).getListResult().get(i).getValue().substring(0,21)+"\n");
                    viewHolder.tv2.append(mlists.get(position).getListResult().get(i).getValue().substring(21,41)+"\n");
                    viewHolder.tv2.append(mlists.get(position).getListResult().get(i).getValue().substring(41)+"\n");
                }
                else if(mlists.get(position).getListResult().get(i).getValue().length()>45 && mlists.get(position).getListResult().get(i).getValue().length()<=60){
                    viewHolder.tv1.append(mlists.get(position).getListResult().get(i).getKey()+"\n"+"\n"+"\n"+"\n");
                    viewHolder.tv2.append(mlists.get(position).getListResult().get(i).getValue().substring(0,18)+"\n"
                            +mlists.get(position).getListResult().get(i).getValue().substring(18,31)+"\n"
                            +mlists.get(position).getListResult().get(i).getValue().substring(31,46)+"\n"
                            +mlists.get(position).getListResult().get(i).getValue().substring(46)+"\n");
                }
                else if(mlists.get(position).getListResult().get(i).getValue().length()>60 && mlists.get(position).getListResult().get(i).getValue().length()<=75){
                    viewHolder.tv1.append(mlists.get(position).getListResult().get(i).getKey()+"\n"+"\n"+"\n"+"\n"+"\n");
                    viewHolder.tv2.append(mlists.get(position).getListResult().get(i).getValue().substring(0,18)+"\n"
                            +mlists.get(position).getListResult().get(i).getValue().substring(18,31)+"\n"
                            +mlists.get(position).getListResult().get(i).getValue().substring(31,46)+"\n"
                            +mlists.get(position).getListResult().get(i).getValue().substring(46,61)+"\n"
                            +mlists.get(position).getListResult().get(i).getValue().substring(61)+"\n");
                }
                else if(mlists.get(position).getListResult().get(i).getValue().length()>75 && mlists.get(position).getListResult().get(i).getValue().length()<=90){
                    viewHolder.tv1.append(mlists.get(position).getListResult().get(i).getKey()+"\n"+"\n"+"\n"+"\n"+"\n"+"\n");
                    viewHolder.tv2.append(mlists.get(position).getListResult().get(i).getValue().substring(0,18)+"\n"
                            +mlists.get(position).getListResult().get(i).getValue().substring(18,31)+"\n"
                            +mlists.get(position).getListResult().get(i).getValue().substring(31,46)+"\n"
                            +mlists.get(position).getListResult().get(i).getValue().substring(46,61)+"\n"
                            +mlists.get(position).getListResult().get(i).getValue().substring(61,76)+"\n"
                            +mlists.get(position).getListResult().get(i).getValue().substring(76)+"\n");
                }
            }
        }
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.tv1)
        TextView tv1;
        @BindView(R.id.tv2)
        TextView tv2;
        @BindView(R.id.ll)
        RelativeLayout ll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

